/*import i18n from "@root/i18n";

// 当前i18n配置已支持语言自动切换，客户端下默认会跟随用户浏览器语言设置。例如，若用户浏览器语言为 zh-CN，则界面将显示为简体中文。
console.log("(client)：", i18n.t("welcome_game"));
console.log("(client)：", i18n.t("welcome_ap"));
console.log(
  "(client)：",
  i18n.t("navigator.language", { language: navigator.language })
);
*/

let tasks = storage.get('tasks') || [];
let taskHistory = storage.get('taskHistory') || {};

// 监听玩家右键点击事件
world.onPlayerJoin((player) => {
  // 监听玩家右键点击
  player.events.on('rightClick', () => {
    // 发送菜单显示事件到客户端
    remoteChannel.sendClientEvent(player, 'showTaskMenu', {});
  });
});

// 处理发布任务
remoteChannel.onServerEvent('publishTask', (player, data) => {
  const taskId = `task_${Date.now()}`;
  const newTask = {
    id: taskId,
    title: data.title,
    content: data.content,
    publisher: player.name,
    publisherId: player.id,
    status: 'available', // available, accepted, completed
    createdAt: Date.now(),
    acceptor: null,
    acceptorId: null,
  };

  tasks.push(newTask);
  storage.set('tasks', tasks);

  // 添加到发布者历史
  if (!taskHistory[player.id]) {
    taskHistory[player.id] = { published: [], accepted: [] };
  }
  taskHistory[player.id].published.push(taskId);
  storage.set('taskHistory', taskHistory);

  // 通知发布成功
  remoteChannel.sendClientEvent(player, 'taskPublished', { success: true });
});

// 处理接取任务
remoteChannel.onServerEvent('acceptTask', (player, data) => {
  const task = tasks.find((t) => t.id === data.taskId);
  if (task && task.status === 'available') {
    task.status = 'accepted';
    task.acceptor = player.name;
    task.acceptorId = player.id;

    // 更新存储
    storage.set('tasks', tasks);

    // 添加到接取者历史
    if (!taskHistory[player.id]) {
      taskHistory[player.id] = { published: [], accepted: [] };
    }
    taskHistory[player.id].accepted.push(task.id);
    storage.set('taskHistory', taskHistory);

    // 发送消息给发布者
    const message = {
      type: 'task_accepted',
      taskId: task.id,
      taskTitle: task.title,
      acceptorName: player.name,
      acceptorId: player.id,
      timestamp: Date.now(),
    };

    // 存储消息到发布者
    let publisherMessages = storage.get(`messages_${task.publisherId}`) || [];
    publisherMessages.push(message);
    storage.set(`messages_${task.publisherId}`, publisherMessages);

    // 通知发布者有新消息
    const publisher = world
      .querySelectorAll('player')
      .find((p) => p.id === task.publisherId);
    if (publisher) {
      remoteChannel.sendClientEvent(publisher, 'newMessage', {});
    }

    remoteChannel.sendClientEvent(player, 'taskAccepted', { success: true });
  }
});

// 获取任务列表
remoteChannel.onServerEvent('getTaskList', (player, data) => {
  const availableTasks = tasks.filter((task) => task.status === 'available');
  remoteChannel.sendClientEvent(player, 'taskList', { tasks: availableTasks });
});

// 获取任务详情
remoteChannel.onServerEvent('getTaskDetail', (player, data) => {
  const task = tasks.find((t) => t.id === data.taskId);
  remoteChannel.sendClientEvent(player, 'taskDetail', { task });
});

// 获取历史记录
remoteChannel.onServerEvent('getHistory', (player, data) => {
  const history = taskHistory[player.id] || { published: [], accepted: [] };
  const publishedTasks = tasks.filter((task) =>
    history.published.includes(task.id)
  );
  const acceptedTasks = tasks.filter((task) =>
    history.accepted.includes(task.id)
  );

  remoteChannel.sendClientEvent(player, 'historyData', {
    published: publishedTasks,
    accepted: acceptedTasks,
  });
});

// 获取消息
remoteChannel.onServerEvent('getMessages', (player, data) => {
  const messages = storage.get(`messages_${player.id}`) || [];
  remoteChannel.sendClientEvent(player, 'messagesData', { messages });
});
